package lgs.hm.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import lgs.hm.mapper.DeptMapper;
import lgs.hm.mapper.EmployeeMapper;
import lgs.hm.model.Dept;
import lgs.hm.model.Employee;
import lgs.hm.service.EmployeeService;

@Controller
@RequestMapping("employee")
public class EmployeeController {
	@Resource
	private EmployeeService employeeService;
	@Resource
	private EmployeeMapper employeeMapper;
	
	
	@GetMapping("findallemp")
	public String findAllEmp(Map<String, Object> map) {
		List<Employee> emp = employeeService.findAllEmp();
		map.put("list", emp);
		return "findallemp";
	}
	
	@GetMapping("findall8")
	public String findAll8(Integer number,Map<String, Object> map) {
		List<Employee> emp = employeeService.findAll8(number);
		map.put("list", emp);
		return "findallemp";
	}
	
	@Resource
	private DeptMapper deptMapper;
	
	@GetMapping("insertemp")
	public String insertEmp(Employee emp,Map<String, Object> map) {
		if(emp.getName()==null) {
			List<Dept> dept = deptMapper.allDept();
			map.put("deptlist", dept);
			return "insertemp";
		}
		employeeService.insertEmp(emp);
		return "redirect:findallemp";
	}
	
	@GetMapping("updateemp")
	public String updateEmp(Employee emp,Map<String, Object> map) {
		if(emp.getSalary()==null) {
			List<Dept> dept = deptMapper.allDept();
			map.put("deptlist", dept);
			return "updateemp";
		}
		employeeService.updateEmp(emp);
		return "redirect:findallemp";
	}
	
	@GetMapping("deleteemp")
	public String deleteEmp(Integer min,Integer max,Map<String, Object> map) {
		if(min==null||max==null) {
			return "deleteemp";
		}
		employeeService.deleteEmp(min, max);
		return "redirect:findallemp";
		
	}
	
	
	
	
	
	
	
	
	
	
	@GetMapping("all")
//	@ResponseBody
	public String findAll(Map<String, Object> map) throws Exception{
			 List<Employee> emp = employeeService.findAll();
			 System.out.println("==========="+emp);
			map.put("list", emp);
			 return "emp";
	}
	
	@GetMapping("id")
	@ResponseBody
	public Object findById(@RequestParam("id")int id) {
		return employeeMapper.findById(id);
	}
	
	@GetMapping("insert")
	public String insert(String phone,String name,double salary,int age) {
		Employee map = new Employee();
		map.setAge(age);
		map.setName(name);
		map.setPhone(phone);
		map.setSalary(salary);
		employeeService.insert(map);
		return "redirect:all";
	}
	
	@GetMapping("update")
	public String update(Integer id,String phone,String name,Double salary,Integer age) {
		Employee map = new Employee();
		map.setId(id);
		map.setPhone(phone);
		map.setName(name);
		map.setSalary(salary);
		map.setAge(age);
		employeeService.update(map);
		return "redirect:all";
	}
	
	@GetMapping("delet")
	public String delet(Integer id) {
		Employee map = new Employee();
		map.setId(id);
		employeeService.delet(map);
		return "redirect:all";
	}
	
	
	//一对一级联查询的两种方式
	//第一种
	@GetMapping("select")
	@ResponseBody
	public List<Employee> select() throws Exception{
		return employeeService.select();
	}
	//第二种
	@GetMapping("all_two")
	@ResponseBody
	public Object finTwo() {
		return employeeMapper.findAllTwo();
	}
	
	
	

}
